package com.cj.webapp.common.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

/**
 * Created by dell on 2017/12/25.
 */
public class MybatisUtil {
    private static SqlSessionFactory sessionFactory;
    private static Reader reader;
    static {
        try {
            //mybatis的配置文件
            String resource = "mybatis-config.xml";
            //使用MyBatis提供的Resources类加载mybatis的配置文件（它也加载关联的映射文件）
            reader = Resources.getResourceAsReader(resource);
            //构建sqlSession的工厂
            sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getInstance() {
        return sessionFactory;
    }


}
